package com.botarena.client.account;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;

import com.botarena.client.UIHelper;
import com.botarena.client.account.remote.AccountAccess;
import com.botarena.client.account.remote.AccountAccessAsync;
import com.botarena.client.ui.StringHelper;
import com.botarena.shared.RankInfo;
import com.botarena.shared.UserHomeInfo;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class UserHomepage extends VerticalPanel {
	
	private static final Logger log = Logger.getLogger(UserHomepage.class.getName());
	
	private UserHomeInfo userHomeInfo;
	
	public UserHomepage(String userKey) {
		
		AccountAccessAsync as = GWT.create(AccountAccess.class);

		as.getHomeInfo(userKey, new AsyncCallback<UserHomeInfo>() {

			@Override
			public void onFailure(Throwable caught) {
				clear();
				add(new Label("Sorry, unable to retrieve data..."));
				caught.printStackTrace();
			}

			@Override
			public void onSuccess(UserHomeInfo result) {
				userHomeInfo = result;
				refresh();
			}
		});
		
		clear();
		add(UIHelper.createBigLoader("Retrieving data..."));
	}

	private void refresh() {
		clear();
		
		try {
			Label generalTitle = new Label("General info:");
			generalTitle.setStyleName("title5");
					
			Grid generalTable = new Grid(2, 2);
			generalTable.setStyleName("formTable");
					
			generalTable.setWidget(0, 0, new Label("Name:"));
			generalTable.setWidget(1, 0, new Label("Avatar:"));
							
			generalTable.setText(0, 1, StringHelper.limitedLengthString(userHomeInfo.getName(), 20));
					
			if (userHomeInfo.getAvatar() != null) {
				Image avatar = new Image();
				avatar.setStyleName("avatar");
				avatar.setUrl(userHomeInfo.getAvatar());
							
				generalTable.setWidget(1, 1, avatar);
			} else {
				generalTable.setText(1, 1, "Not uploaded");
			}
			
			Panel generalPanel = new VerticalPanel();
			generalPanel.add(generalTitle);
			generalPanel.add(generalTable);
					
			Panel botsPanel = new VerticalPanel();
			
			Label botsTitle = new Label("Bots:");
			botsTitle.setStyleName("title5");
					
			botsPanel.add(botsTitle);
					
			for (RankInfo ri: userHomeInfo.getBots()) {
				
				Grid riTable = new Grid(5, 2);
				riTable.addStyleName("formTable");
				
				riTable.setWidget(0, 0, new Label("Name:"));
				riTable.setWidget(1, 0, new Label("Rank:"));
				riTable.setWidget(2, 0, new Label("Wins:"));
				riTable.setWidget(3, 0, new Label("Losses:"));
				riTable.setWidget(4, 0, new Label("Draws:"));
				
				riTable.setText(0, 1, StringHelper.limitedLengthString(ri.botName, 20));
				riTable.setText(1, 1, StringHelper.limitedLengthString(""+ri.rank, 20));
				riTable.setText(2, 1, StringHelper.limitedLengthString(""+ri.fightWon, 20));
				riTable.setText(3, 1, StringHelper.limitedLengthString(""+ri.fightLost, 20));
				riTable.setText(4, 1, StringHelper.limitedLengthString(""+ri.fightDraw, 20));
				
				botsPanel.add(riTable);
			}
			
			VerticalPanel panel = new VerticalPanel();
			panel.add(generalPanel);
			panel.add(botsPanel);
					
			panel.setCellHorizontalAlignment(generalPanel, ALIGN_LEFT);
			panel.setCellHorizontalAlignment(botsPanel, ALIGN_LEFT);
					
			clear();
			add(panel);
		} catch (Exception e) {
			add(new Label(e.getMessage()));
		}
		
	}

}
